

this.view = {
	extend: "base",
	put:{
		content: "contents/friendfeed.html"
	}
};

this.handler = function(){
	
	var htmlForm = new HTMLForm.generator(request);
	
	var friendfeedForm = htmlForm.create({
		id: "friendFeedForm",
		method: "post",
		inputs: [
			{"hn-username": {label: "username:", validate: {required: true}}},
			{"hn-password": {label: "password:", type: "password", value: "", validate: {required: true}}},
			{"ff-username": {label: "friendfeed username:", validate: {required: true}}},
			{"ff-remotekey": {label: "remote key:", type: "password",  value: "", validate: {required: true}}},
			{submit_: {value: "authorize", type: "submit"}}
		]
	});
	
	this.get = function(showErrors, errorMessage){
		$("h1, title").append(" : FriendFeed")
		if(errorMessage){
			$("#friendfeed-form").before("<p><span class=error><strong>"+errorMessage+"</strong></span></p>")
		}
		$("#friendfeed-form").html(friendfeedForm.html(showErrors));
		$("form label:eq(0)").before("<img id=hn-logo src=/static/i/hn-logo.png>");
		$("form label:eq(2)").before("<img id=ff-logo src=/static/i/ff-logo.png>");
		
	}
	
	this.post = function(){
		var errorMessage;
		if(friendfeedForm.pass){
			errorMessage = "Could not connect to News.YC.";
			$.ajax({
				url: "http://news.ycombinator.com/",
				async: false,
				complete: function(xhr){
					var loginAnchor = $("table:eq(1) a:last", xhr.responseXML);
					errorMessage = "Could not login to News.YC.";
					if(loginAnchor){
						$.ajax({
							url: "http://news.ycombinator.com/" + loginAnchor.attr("href")+"",
							async: false,
							complete: function(xhr){
								java.net.HttpURLConnection.setFollowRedirects(false);
								$.ajax({
									url: "http://news.ycombinator.com/y",
									async: false,
									type: "POST",
									data:{
										u: friendfeedForm.data["hn-username"],
										p: friendfeedForm.data["hn-password"],
										fnid: $("input[type=hidden]:first", xhr.responseXML).attr("value")+""
									},
									complete: function(xhr, status){
										errorMessage = "Bad Login. Could not login to News.YC.";
										java.net.HttpURLConnection.setFollowRedirects(true);
										var cookie = xhr.getResponseHeader("Set-Cookie");
										if(cookie){
											$.ajax({
												url: "http://friendfeed.com/api/validate",
												async: false,
												username: friendfeedForm.data["ff-username"],
												password: friendfeedForm.data["ff-remotekey"],
												error: function(){
													errorMessage = "Could not connect to FriendFeed";
												},
												complete: function(xhr, status){
													errorMessage = "FreindFeed authoriazation failed. Please check FriendFeed username and remotekey.";
													if(xhr.status == 200){
														var user;
														if(!(user = storage.users.getSingle({"hn-username":  friendfeedForm.data["hn-username"]}))){
															storage.users.insert({
																"hn-username": friendfeedForm.data["hn-username"],
																"hn-cookie": cookie,
																"ff-username":friendfeedForm.data["ff-username"],
																"ff-remotekey":friendfeedForm.data["ff-remotekey"]
															});
														} else {
															storage.users.update({
																"hn-cookie": cookie,
																"ff-username":friendfeedForm.data["ff-username"],
																"ff-remotekey":friendfeedForm.data["ff-remotekey"]
															},{"hn-username":  friendfeedForm.data["hn-username"]});																
														}
														response.redirect("./friendfeed/authorized/"+ friendfeedForm.data["hn-username"] );
													}
												}
											});
										}
									}
								});
							}
						});
					}
				}
			});
		} 
		this.get(!friendfeedForm.pass, errorMessage);
	}
	
}
	
